package com.ss.web.tagmark.init;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

public class WebSecurityRequestMatcher implements RequestMatcher {
	private List<AntPathRequestMatcher> ms;

	public WebSecurityRequestMatcher(String... antPaths) {
		if (ArrayUtils.isEmpty(antPaths)) {
			throw new IllegalArgumentException("缺少antPaths参数");
		}
		ms = new ArrayList<>();
		for (String p : antPaths) {
			if (StringUtils.isBlank(p)) {
				continue;
			}
			ms.add(new AntPathRequestMatcher(p.trim()));
		}
		if (ms.isEmpty()) {
			throw new IllegalArgumentException("缺少antPaths参数");
		}
	}

	public static WebSecurityRequestMatcher getMatcherInstance(String... antPaths) {
		return new WebSecurityRequestMatcher(antPaths);
	}

	@Override
	public boolean matches(HttpServletRequest request) {
		for (AntPathRequestMatcher m : ms) {
			if (m.matches(request)) {
				return true;
			}
		}
		return false;
	}

}
